package it.unical.mat.aec.gui;


import it.unical.mat.aec.manageResult.ManageResultFacade;
import it.unical.mat.aec.model.Traccia;
import it.unical.mat.aec.persistence.PersistanceException;
import it.unical.mat.aec.processTests.CorrezioneException;
import it.unical.mat.aec.processTests.FileNonValidoException;
import it.unical.mat.aec.processTests.LettoreFileTestException;
import it.unical.mat.aec.processTests.ProcessTestsFacade;
import it.unical.mat.aec.processTests.TipoInputFile;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.print.PrinterException;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class CorrezionePanel extends JPanel{
	
	private JTable studentTable;
	private boolean correzioneEffettuata;
	
	public CorrezionePanel(){
		super(new BorderLayout());
		correzioneEffettuata=false;
		JSplitPane pane=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		pane.setOneTouchExpandable(false);
		studentTable=new JTable(new TestValutationTableModel(ProcessTestsFacade.getInstance().getTests()));
		studentTable.setDefaultRenderer(Object.class, new JTableRender());
		pane.add(new JScrollPane(studentTable), JSplitPane.RIGHT);
		JPanel upPanel=new JPanel();
		upPanel.setLayout(new BorderLayout());
		JPanel panel=new JPanel();
		upPanel.add(panel,BorderLayout.WEST);
		pane.add(upPanel, JSplitPane.LEFT);
		addComponent(panel);
		add(pane,BorderLayout.CENTER);
	}

	private void addComponent(JPanel panel) {
		Dimension dimension=new Dimension(80, 80);

		panel.add(IconButton.getLabelButton("images/icon/correggi.png", dimension,"Correggi",new LabelButtonEvent() {
			
			@Override
			public void event() {
				try {
					ProcessTestsFacade.getInstance().correggiTest();
				} catch (CorrezioneException e) {
					JOptionPane.showMessageDialog(CorrezionePanel.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
				}
				CorrezionePanel.this.repaint();
				correzioneEffettuata=true;
			}
		}));
		panel.add(IconButton.getLabelButton("images/icon/aggiungiTest.png", dimension,"Aggiungi Test",new LabelButtonEvent() {
			
			@Override
			public void event() {
				ProcessTestsFacade facade=ProcessTestsFacade.getInstance();
				File files[]=CaricaFile.getFile(CorrezionePanel.this);
				if(files!=null){
						try {
							facade.acquisciEsami(files, TipoInputFile.XML);
						} catch (LettoreFileTestException e) {
							int response=JOptionPane.showConfirmDialog(CorrezionePanel.this,"Error :"+e.getMessage()+"\n\nVuoi continuare ?");
							if(response==JOptionPane.NO_OPTION)
								return ;
						} catch (FileNonValidoException e) {
							JOptionPane.showMessageDialog(CorrezionePanel.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
							return ;
						}
						Traccia tracciaDaApplicare=facade.getTests().get(0).getTraccia();
						try {
							facade.associaTraccia(tracciaDaApplicare.getId());
						} catch (PersistanceException e) {
							JOptionPane.showMessageDialog(CorrezionePanel.this, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
						}
				}
				studentTable.setModel(new TestValutationTableModel(ProcessTestsFacade.getInstance().getTests()));
				repaint();
			}
		}));
		panel.add(IconButton.getLabelButton("images/icon/salva.png", dimension,"Salva",new LabelButtonEvent() {
			
			@Override
			public void event() {
				String path=CaricaFile.getDirectory(CorrezionePanel.this);
				if(path!=null){
					if(ProcessTestsFacade.getInstance().salvaTests(path+".aec"))
						JOptionPane.showMessageDialog(CorrezionePanel.this,"Salvataggio completato" , "Informazione", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}));
		panel.add(IconButton.getLabelButton("images/icon/stampa.png",dimension,"Stampa tests",new LabelButtonEvent() {
			
			@Override
			public void event() {
				try {
					if(studentTable.print())
						JOptionPane.showMessageDialog(CorrezionePanel.this,"Avvio Stampa" , "Informazione", JOptionPane.INFORMATION_MESSAGE);					
					else
						JOptionPane.showMessageDialog(CorrezionePanel.this,"Stampa non effettuata" , "Informazione", JOptionPane.INFORMATION_MESSAGE);
				} catch (PrinterException e) {
					JOptionPane.showMessageDialog(CorrezionePanel.this,e.getLocalizedMessage() , "Error", JOptionPane.ERROR_MESSAGE);					
				}
			}
		}));
		panel.add(IconButton.getLabelButton("images/icon/modifica.png",dimension,"Vai in Modifica Tests",new LabelButtonEvent() {
			
			@Override
			public void event() {
				if(!correzioneEffettuata){
					JOptionPane.showMessageDialog(CorrezionePanel.this,"Effettua prima una correzione prima di andare a Modifica Correzioni", "Info", JOptionPane.INFORMATION_MESSAGE);										
				}else{
					ManageResultFacade.getInstance().setTests(ProcessTestsFacade.getInstance().getTests());
					MainFrame.getInstance().modificaPanel();
				}
			}
		}));
	}
	
}

class JTableRender extends DefaultTableCellRenderer {  
	   public Component getTableCellRendererComponent(JTable table,   
	   Object value, boolean isSelected, boolean hasFocus, int row, int column) {  
	     JLabel parent = (JLabel) super.getTableCellRendererComponent(table,   
	      value, isSelected, hasFocus, row, column);
	     setHorizontalAlignment(JLabel.LEFT);
	      if(column==table.getColumnCount()-1 ){
	    	  setHorizontalAlignment(JLabel.CENTER);
	    	  parent.setFont( parent.getFont().deriveFont(Font.ITALIC));
	      }
	     return parent;  
	   }      
}
